package com.xlh.virtualization.service.impl;

import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.xlh.aop.PreAcquireLock;
import com.xlh.virtualization.dao.StatisticMapper;
import com.xlh.virtualization.dao.helper.StatisticMapperHelper;
import com.xlh.virtualization.domain.Statistic;
import com.xlh.virtualization.service.ExperimentStatisticService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.xlh.virtualization.constant.StatisticConstant.SAVE_EXPERIMENT_STATISTIC_KEY;

/**
 * @author cheer
 */
@Service
public class ExperimentStatisticServiceImpl implements ExperimentStatisticService {

    private static final String[] CHINESE_CHAR = new String[]{"天", "小时", "分", "秒"};
    private static final String[] ENGLISH_CHAR = new String[]{"D", "H", "M", "S"};

    @Autowired
    private StatisticMapper statisticMapper;

    @Autowired
    private StatisticMapperHelper statisticMapperHelper;

    @Override
    @PreAcquireLock(SAVE_EXPERIMENT_STATISTIC_KEY)
    public void saveExperimentDuration(Long userId, Integer contextType, Long contextId, Date startTime, Date endTime) {
        Statistic statistic = statisticMapperHelper.getByUserIdAndContext(userId, contextType, contextId);

        DateBetween dateBetween = new DateBetween(startTime, endTime);
        long experimentTime = dateBetween.between(DateUnit.SECOND);

        if (statistic == null) {
            Statistic insertStatistic = Statistic.builder()
                    .userId(userId)
                    .contextType(contextType)
                    .contextId(contextId)
                    .experimentTime(experimentTime)
                    .build();
            statisticMapper.insertSelective(insertStatistic);
        } else {
            Statistic updateStatistic = Statistic.builder()
                    .id(statistic.getId())
                    .experimentTime(experimentTime + statistic.getExperimentTime())
                    .build();
            statisticMapper.updateByPrimaryKeySelective(updateStatistic);
        }
    }

    @Override
    public String getExperimentDuration(Long userId) {
        List<Statistic> statisticList = statisticMapperHelper.listByUserId(userId);

        long experimentTime = 0;
        for (Statistic statistic : statisticList) {
            experimentTime += statistic.getExperimentTime();
        }

        String chineseExperimentTime = DateUtil.formatBetween(experimentTime * 1000, BetweenFormater.Level.SECOND);
        String englishExperimentTime = StringUtils.replaceEach(chineseExperimentTime, CHINESE_CHAR, ENGLISH_CHAR);

        if (englishExperimentTime.contains("D")) {
            if (englishExperimentTime.lastIndexOf('H') == -1) {
                return englishExperimentTime.substring(0, englishExperimentTime.lastIndexOf('D') + 1);
            } else {
                return englishExperimentTime.substring(0, englishExperimentTime.lastIndexOf('H') + 1);
            }
        } else if (englishExperimentTime.contains("H")) {
            if (englishExperimentTime.lastIndexOf('M') == -1) {
                return englishExperimentTime.substring(0, englishExperimentTime.lastIndexOf('H') + 1);
            } else {
                return englishExperimentTime.substring(0, englishExperimentTime.lastIndexOf('M') + 1);
            }
        } else {
            return englishExperimentTime;
        }
    }
}
